package IO;

import java.io.File;
import java.util.Scanner;

public class IO17 {
    private static void scan(File currentFile,String key){
        if(!currentFile.isDirectory()){
            return;
        }
        File[] files = currentFile.listFiles();
        if(files==null||files.length==0){
            return;
        }
        for(File f:files){
            if(f.isFile()){
                doDelete(f,key);
            }else {
                scan(f,key);
            }
        }
    }
    private static void doDelete(File f, String key){
        if(!f.getName().contains(key)){
            return;
        }
        Scanner scanner = new Scanner(System.in);
        System.out.println(f.getAbsolutePath()+"是否确认要删除 Y/n");
        String choice = scanner.next();
        if(choice.equals("Y")||choice.equals("y")){
            f.delete();
        }
    }
    public static void main(String[] args) {
        System.out.println("请输入要搜索的路径:");
        Scanner scanner = new Scanner(System.in);
        String rootPath = scanner.next();
        File rootFile = new File(rootPath);
        if(!rootFile.isDirectory()){
            System.out.println("输入路径不存在");
            return;
        }
        System.out.println("请输入要删除的文件名字的关键字");
        String key =  scanner.next();
        //递归查找
        scan(rootFile,key);
    }
}
